import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-11-10
 * Time: 21:05
 */
public class RedisDemoHash {

    public static void test1(Jedis jedis) {
        System.out.println("jset 和 hget");
        jedis.flushAll();

        // 单个设置键值对
        jedis.hset("key", "f1", "111");
        // 多个设置
        Map<String, String> fields = new HashMap<>();
        fields.put("f2", "222");
        fields.put("f3", "333");
        jedis.hset("key", fields);

        String result = jedis.hget("key", "f1");
        System.out.println("result:" +  result);

        result = jedis.hget("key", "f2");
        System.out.println("result:" +  result);

        result = jedis.hget("key", "f3");
        System.out.println("result:" +  result);

        result = jedis.hget("key", "f4");
        System.out.println("result:" +  result);
    }


    public static void test2(Jedis jedis) {
        System.out.println("hexists");
        jedis.flushAll();

        // 单个设置键值对
        jedis.hset("key", "f1", "111");
        // 多个设置
        Map<String, String> fields = new HashMap<>();
        fields.put("f2", "222");
        fields.put("f3", "333");
        jedis.hset("key", fields);

        boolean result = jedis.hexists("key", "f1");
        System.out.println("result:" + result);

        result = jedis.hexists("key", "f2");
        System.out.println("result:" + result);

        result = jedis.hexists("key", "f3");
        System.out.println("result:" + result);

        result = jedis.hexists("key", "f4");
        System.out.println("result:" + result);

    }


    public static void test3(Jedis jedis) {
        System.out.println("hdel");
        jedis.flushAll();

        // 单个设置键值对
        jedis.hset("key", "f1", "111");
        // 多个设置
        Map<String, String> fields = new HashMap<>();
        fields.put("f2", "222");
        fields.put("f3", "333");
        jedis.hset("key", fields);

        long result = jedis.hdel("key", "f1", "f2", "f4");
        System.out.println("result:" + result);

        boolean result1 = jedis.hexists("key", "f1");
        System.out.println("result1:" + result1);

        result1 = jedis.hexists("key", "f2");
        System.out.println("result1:" + result1);

        result1 = jedis.exists("key");
        System.out.println("result1:" + result1);

    }



    public static void test4(Jedis jedis) {
        System.out.println("hkeys 和 hvals");
        jedis.flushAll();

        // 单个设置键值对
        jedis.hset("key", "f1", "111");
        // 多个设置
        Map<String, String> fields = new HashMap<>();
        fields.put("f2", "222");
        fields.put("f3", "333");
        jedis.hset("key", fields);

        Set<String> fs = jedis.hkeys("key");
        List<String> vs = jedis.hvals("key");
        System.out.println("fields:" + fs);
        System.out.println("values:" + vs);


    }

    public static void test5(Jedis jedis) {
        System.out.println("hkeys 和 hvals");
        jedis.flushAll();


        // 多个设置
        Map<String, String> fields = new HashMap<>();
        fields.put("f2", "222");
        fields.put("f3", "333");
//        jedis.hset("key", fields);
        // 上下等价
        jedis.hmset("key", fields);

        // 拿到的顺序和后面的 fields 相同
        List<String> result = jedis.hmget("key", "f1", "f2", "f3");
        System.out.println("result:" + result);

    }


    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()) {
            test5(jedis);
        }
    }
}
